package com.arm.service.impl;

import com.arm.dao.ProductTypeMapper;
import com.arm.module.ProductType;
import com.arm.service.ProductTypeService;
import com.arm.tools.myRunTime.MyRunTimeExcption;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by lutao on 2017/7/19.
 */
@Service
public class ProductTypeServiceImpl implements ProductTypeService {
    @Autowired
    ProductTypeMapper productTypeMapper;

    @Override
    public List<ProductType> getProductTypeList(Map<String, String> map) {
        return productTypeMapper.getProductTypeList(map);
    }

    @Override
    public void modifyProductType(ProductType productType) {
        String productTypeName = productType.getProductTypeName();
        if(StringUtils.isEmpty(productTypeName)){
            throw new MyRunTimeExcption("请输入产品类型");
        }

        if(productType.getTid()==null){
            productType.setParentId(0L);
            productTypeMapper.insertProductType(productType);
        }

        if(productType.getTid()!=null){
            ProductType productType1 ;
            productType1 =  productTypeMapper.selectByPrimaryKey(productType.getTid());
            if(productType.getParentId()!=null){
                productType1.setParentId(productType.getParentId());
            }
            if(productType.getProductTypeName()!=null){
                productType1.setProductTypeName(productType.getProductTypeName());
            }

            if(productType.getSort()!=null){
                productType1.setSort(productType.getSort());
            }
            productTypeMapper.updateProductTypeByPrimaryKeySelective(productType1);
        }
    }

    @Override
    public ProductType selectByPrimaryKey(Long tid) {
        return productTypeMapper.selectByPrimaryKey(tid);
    }

    @Override
    public List<ProductType> getParentTypeList(Map<String, String> map) {
        return productTypeMapper.getParentTypeList(map);
    }

    @Override
    public PageInfo getPageInfoProductTypeList(Map<String, String> map) {
        Integer page = Integer.parseInt(map.get("page"));
        Integer pageSize = Integer.parseInt(map.get("pageSize"));
        PageHelper.startPage(page, pageSize);
        List<ProductType> productTypeList = productTypeMapper.getProductTypeList(map);
        PageInfo<ProductType> pageinfo = new PageInfo<>(productTypeList);
        return pageinfo;
    }
}
